﻿using System;
using System.Collections.Generic;
using System.Text;
using FbHelperWP7NonUi.Settings;

namespace FbHelperWP7NonUi.TextHelpers
{
    public sealed class SimpleText : AbstractTextPart
    {
        private static readonly string[] emptySplitted = new string[] { Environment.NewLine };
        private static TextStyle style = GetStartStyleFromSettings();

        private static SimpleText emptyParagraph = new SimpleText( String.Empty );

        private readonly string originalText;
        private readonly string text;

        private readonly string[] splitted;

        public SimpleText( string text )
        {
            originalText = text;
            this.text = text.Trim() + Environment.NewLine;

            splitted = text.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );

            if (splitted.Length == 0)
                splitted = emptySplitted;
        }

        public string Text
        {
            get
            {
                return originalText;
            }
        }

        public override StyledText CreateTextVisual( int startWord, int wordCount )
        {
            if (wordCount < 0)
                throw new ArgumentException( String.Format( "Parameter should be not less 0 (now it is {0})", wordCount ), "wordCount" );

            if (startWord < 0)
                throw new ArgumentException( String.Format( "Parameter should be not less 0 (now it is {0})", startWord ), "startWord" );

            //ToDo: optimize it using caching

            return new StyledText( GetText( startWord, wordCount ), style );
        }

        private string GetText( int startWord, int wordCount )
        {
            if (startWord == 0 && wordCount >= splitted.Length)
                return text;

            StringBuilder resultBuilder = new StringBuilder();

            for (int i = startWord; i < wordCount && i < splitted.Length; i++)
            {
                resultBuilder.Append( splitted[i] );
                resultBuilder.Append( " " );
            }

            string result = resultBuilder.ToString();

            if (string.IsNullOrWhiteSpace( result ))
                return String.Empty;

            result = result.Substring( 0, result.Length - 1 );

            if (wordCount > splitted.Length)
                result = result + Environment.NewLine;

            return result;
        }

        public override int GetWordCount()
        {
            return splitted.Length;
        }

        public static SimpleText EmptyParagraph
        {
            get
            {
                return emptyParagraph;
            }
        }


        private static TextStyle GetStartStyleFromSettings()
        {
            TextStyleSettings settings = ProgramState.CurrentState.TextStyle;

            settings.StylesChangedEvent += new TextStyleSettings.StylesChanged( settings_StylesChangedEvent );

            return new TextStyle( settings.SimpleTextStyle );
        }

        static void settings_StylesChangedEvent()
        {
            style = new TextStyle( ProgramState.CurrentState.TextStyle.SimpleTextStyle );
            emptyParagraph = new SimpleText( String.Empty );
        }
    }
}
